//
//  RefreshRingHeader.m
//  Refresh
//
//  Created by ning liu on 2018/3/13.
//  Copyright © 2018年 ning liu. All rights reserved.
//

#import "RefreshRingHeader.h"

@implementation RefreshRingHeader

- (void)setupProperties{
    [super setupProperties];
    [self.layer addSublayer:self.arcLayer];
    [self addSubview:self.icon];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.arcLayer.frame = CGRectMake(0, 0, self.kaf_width, self.kaf_height);
    self.icon.center = CGPointMake(self.kaf_width/2., self.kaf_height/2.);
}

- (void)setFillColor:(UIColor *)fillColor{
    if (super.fillColor == fillColor) {
        return;
    }
    [super setFillColor:fillColor];
}

- (void)setAnimatedBackgroundColor:(UIColor *)animatedBackgroundColor{
    if (super.animatedBackgroundColor == animatedBackgroundColor) {
        return;
    }
    [super setAnimatedBackgroundColor:animatedBackgroundColor];
}

- (void)kafkaDidScrollWithProgress:(CGFloat)progress max:(const CGFloat)max{
#define kOffset 0.7
    
    if (progress >= kOffset) {
        progress = (progress-kOffset)/(max - kOffset);
        [self.arcLayer setProgress:progress];
    }
}

- (void)kafkaRefreshStateDidChange:(KafkaRefreshState)state{
    [super kafkaRefreshStateDidChange:state];
    switch (state) {
        case KafkaRefreshStateNone:{
            [self.arcLayer setProgress:0];
            break;
        }
        case KafkaRefreshStateScrolling:
        case KafkaRefreshStateReady:{
            break;
        }
        case KafkaRefreshStateRefreshing:{
            [self.arcLayer startAnimating];
            break;
        }
        case KafkaRefreshStateWillEndRefresh:{
            [self.arcLayer stopAnimating];
            break;
        }
    }
}

- (RefreshIconLayer *)arcLayer{
    if (!_arcLayer) {
        _arcLayer = [RefreshIconLayer layer];
    }
    return _arcLayer;
}

- (UIImageView *)icon{
    if (!_icon) {
        _icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        _icon.image = [UIImage imageNamed:@"refresh_icon"];
    }
    return _icon;
}

@end
